# frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :check_users_quota

  def create
    build_resource(sign_up_params)

    unless verify_captcha?(resource)
      clean_up_passwords resource
      respond_with resource
      return
    end

    resource.save
    yield resource if block_given?
    if resource.persisted?
      session[:omniauth] = nil
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end

  def after_inactive_sign_up_path_for(resource_or_scope)
    new_user_session_path
  end

  def check_users_quota
    # PRO-begin
    License.check_users_limit!
    # PRO-end
  end
end
